// pages/record/record.js
const db = wx.cloud.database({
  env: 'work1-0g1b3grp436e9236'
})

Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '所有记录',
        jobList: [],
        loading: true,
        isNull: false
    },

    async getJobList() {
      const $ = db.command.aggregate
      let res = await db.collection('work_record_job').aggregate().match({userId: wx.getStorageSync('userInfo').userId}).group({
        _id: '$viewTime',
        recordList: $.push({
          jobId: '$jobId',
          jobName: '$jobName',
          location: '$location',
          needNum: '$needNum',
          remainNum: '$remainNum',
          salary: '$salary',
          content: '$content',
          userId: '$userId',
          updateTime: '$updateTime'
        })
      }).limit(15).end()
      // 最新
      let jobList = this.MsgSort(res.list, '_id')
      for (let i = 0; i < jobList.length; i++) {
          jobList[i].recordList = jobList[i].recordList.sort((a, b)=>{
              let t1 = a.updateTime
              let t2 = b.updateTime
              return t2.getTime()-t1.getTime()
          })
      }
      this.setData({
          jobList: this.data.jobList.concat(jobList),
          loading: false
      })
      if (this.data.jobList.length === 0) {
          this.setData({isNull: true})
      }
    //   console.log(this.data.jobList)
    },

    //根据时间戳排序
    MsgSort(obj, by){
        obj.sort((a,b)=>{
            let t1 = new Date(Date.parse(a[by].replace(/-/g,"/")))
            let t2 = new Date(Date.parse(b[by].replace(/-/g,"/")))
            return t2.getTime()-t1.getTime()
        })
        return obj
    },

    toJobDetailPage(e) {
      console.log(e.currentTarget.dataset.id)
      wx.navigateTo({
          url: '/pages/job-detail/job-detail?id=' + e.currentTarget.dataset.id
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getJobList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})